package com.xxc.hs;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.xxc.hs.db.UserHelper;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

@SuppressWarnings("ALL")
public class RegistActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private EditText et_name, et_pwd;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);

        try {
            initView();

            //返回事件
            toolbar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });

            findViewById(R.id.btn_regist).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String name = et_name.getText().toString();
                    String pwd = et_pwd.getText().toString();
                    if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) {
                        Toast.makeText(RegistActivity.this, "注册失败,用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                    } else {
                        int ids = UserHelper.getInstance(RegistActivity.this).register(name, pwd, 0);
                        if (ids > 0) {
                            Toast.makeText(RegistActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(RegistActivity.this, "注册失败，用户名重复", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initView() {
        toolbar = findViewById(R.id.toolBar);
        et_name = findViewById(R.id.et_regist_name);
        et_pwd = findViewById(R.id.et_regist_pwd);
    }
}
